//
//  HotColumnViewController.m
//  礼物go
//
//  Created by dllo on 16/5/16.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "HotColumnViewController.h"

#import "SearchViewController.h"
#import "HotDeatilViewController.h"


#define MARGINSPACE 10
#define ITEMHEIGHT 260

@interface HotColumnViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>




@end

@implementation HotColumnViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"热门";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(rightButtonAction)];
    
    self.dataArray = [NSMutableArray array];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((SCREENWIDTH - MARGINSPACE * 3) / 2, ITEMHEIGHT);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
flowLayout.sectionInset = UIEdgeInsetsMake(MARGINSPACE, MARGINSPACE , 0, MARGINSPACE);
    
    self.hotCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, self.view.frame.size.height - 49) collectionViewLayout:flowLayout];
    self.hotCollectionView.dk_backgroundColorPicker = DKColorPickerWithRGB(0xFFFAFA,0xBEBEBE);
    [self.view addSubview:self.hotCollectionView];
    self.hotCollectionView.dataSource = self;
    self.hotCollectionView.delegate = self;
    [self.hotCollectionView registerClass:[HotColumnCell class] forCellWithReuseIdentifier:hotCell];
    self.hotCollectionView.showsVerticalScrollIndicator = NO;
    
    
    self.isUpLoading = NO;

   [self getSelectList:0];
    [self addHeader];
    [self addFooter];
    
    
    // Do any additional setup after loading the view.
}

#pragma mark --下拉刷新
-(void)addHeader{
    __block HotColumnViewController *hotColumnVC = self;
    // 添加上拉刷新尾部控件
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        hotColumnVC.nextPage = 0;//记录页码
        hotColumnVC.isUpLoading = NO;//标记为下拉操作
        [hotColumnVC getSelectList:hotColumnVC.nextPage];//重新请求数据
    }];
#pragma mark 自动刷新(一进入程序就下拉刷新)
   // [header beginRefreshing];
    self.hotCollectionView.mj_header = header;
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
    NSMutableArray *imageArray = [NSMutableArray array];
    for (int i = 1; i < 6; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"box_0%d",i]];
        [imageArray addObject:image];
    }
    [header setImages:imageArray forState:MJRefreshStateRefreshing];
    
    UIImage *pullImage = [imageArray firstObject];
    NSArray *pullArray = [NSArray arrayWithObjects:pullImage, nil];
    [header setImages:pullArray forState:MJRefreshStatePulling];
    
}

#pragma mark --上拉加载更多
-(void)addFooter{
    __block HotColumnViewController *hotVC = self;
    // 添加上拉刷新尾部控件
    self.hotCollectionView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        hotVC.nextPage ++;
        hotVC.isUpLoading = YES;//标记为上拉操作
        [hotVC getSelectList:hotVC.nextPage];//请求数据
        
    }];
}

-(void)getSelectList:(NSUInteger)nextCursor{
    AFNetworkReachabilityManager *netWorkManger = [AFNetworkReachabilityManager sharedManager];
    //****参数cursor的值: 第一页默认为0，下一页就是请求下来数据中next_cursor的值
    NSString *urlStr = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/items?gender=2&generation=4&limit=50&offset=%ld",50 * nextCursor];
    [LYNetworking ly_GetWithURL:urlStr Parameter:nil success:^(id result) {
        [netWorkManger stopMonitoring];
        if (self.isUpLoading == NO) {
            //说明是下拉，就要清空数组中的数据
            [self.dataArray removeAllObjects];
        }
        NSDictionary *dataDic = [result objectForKey:@"data"];
        NSArray *itemsArray = [dataDic objectForKey:@"items"];
        for (NSDictionary *dic in itemsArray) {
            NSDictionary *smallDic = [dic objectForKey:@"data"];
            HotColumnModel *hotModel = [[HotColumnModel alloc] init];
            [hotModel setValuesForKeysWithDictionary:smallDic];
            [self.dataArray addObject:hotModel];
            //NSLog(@"%@",hotModel.name);
        }
        
        [self.hotCollectionView.mj_header endRefreshing];
        [self.hotCollectionView.mj_footer endRefreshing];
        [self.hotCollectionView reloadData];
        
    } error:^(NSError *error) {
        
    }];


}

-(void)rightButtonAction{
    SearchViewController *searchVC = [[SearchViewController alloc] init];
    searchVC.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:searchVC animated:YES];
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HotColumnCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:hotCell forIndexPath:indexPath];
    HotColumnModel *hotModel = [self.dataArray objectAtIndex:indexPath.row];
    cell.hotModel = hotModel;

    return cell;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    HotColumnModel *hotModel = [self.dataArray objectAtIndex:indexPath.row];
    HotDeatilViewController *hotDetail = [[HotDeatilViewController alloc] init];
    hotDetail.dataId = hotModel.myId;
    hotDetail.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:hotDetail animated:YES];

}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
